<template>
  <div>
    <el-dialog title="分配角色" :visible="showattribute" @close="closeDia">
      <el-checkbox-group v-model="selectRoles">
        <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
          {{ item.name }}
        </el-checkbox>
      </el-checkbox-group>

      <span slot="footer">
        <el-button @click="$emit('update:showattribute', false)">
          取消
        </el-button>
        <el-button type="primary" @click="submitselect">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    showattribute: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      roleList: [],
      selectRoles: [],
      userId: ''
    }
  },
  created () {
    this.getRoles()
  },
  methods: {
    async submitselect () {
      try {
        console.log({ id: this.userId, roleIds: this.selectRoles })
        await assignRoles({ id: this.userId, roleIds: this.selectRoles })
        this.$emit('update:showattribute', false)
        this.$message.success('分配角色成功')
      } catch (error) {
        console.log(error)
      }
    },
    closeDia () {
      this.$emit('update:showattribute', false)
      this.selectRoles = []
    },
    async getRoles () {
      try {
        const res = await getRoleList({ page: 1, pagesize: 999 })
        this.roleList = res.rows
      } catch (error) {
        console.log(error)
      }
    },
    async getSelectedRole (id) {
      try {
        const res = await getUserDetailById(id)
        console.log(res)
        this.selectRoles = res.roleIds || []
        this.userId = id
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
